package org.crmsystem.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

import org.crmsystem.model.SysMsg;

import org.crmsystem.service.ISysMsgService;


import org.crmsystem.service.impl.SysMsgServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import java.util.Date;
import java.util.HashMap;

import java.util.List;

import java.util.Map;


/**
 * Created by Administrator on 2017/8/5.
 */
@Controller

@RequestMapping("/sysmsg")

public class SysMsgController {


    private static Logger logger = LogManager.getLogger(SysMsgServiceImpl.class);


    @Autowired

    private ISysMsgService isms;


    /**
     * 所有消息页面
     *
     * @return the model and view
     */
    @RequestMapping("/allmsg")

    public ModelAndView selectSysMsg() {


        ModelAndView allSysMsg = new ModelAndView("sysMsg");


        return allSysMsg;

    }
    /**
     * 返回所有消息2
     *
     * @return the model and view
     */
    @RequestMapping(value = "/allmsgs")
    @ResponseBody
    public Map selectSysMsgAjax(SysMsg sysMsg, Integer pageSize, Integer currPage) {

        Map map=new HashMap();
        map.put("sysMsg",sysMsg);
        map.put("pageSize",pageSize);
        map.put("currPage", (currPage-1)*pageSize);

        int totalNum=isms.selectByTotalNum(map);
        List listMsg = isms.selectBy(map);

        Map map1=new HashMap();

        map1.put("totalNum",totalNum);
        map1.put("json",listMsg);

        return map1;

    }
    /**
     * 根据id查询消息
     *
     * @return the model and view
     */
    @RequestMapping("/msgById")
    public ModelAndView selectSysMsgById(@RequestParam int id) {

        SysMsg sysMsg=new SysMsg();
        sysMsg.setId(id);
        Map map=new HashMap();
        Map ssmMap = new HashMap<>();
        map.put("sysMsg",sysMsg);

        List<SysMsg> listMsg = isms.selectBy(map);
        for (SysMsg s:listMsg
             ) {
            if (s.getId()==id){
                ssmMap.put("ssm", s);
            }

        }
       //logger.info(listMsg.get(0).getContent()+"====");





        return new ModelAndView("fullMsg", ssmMap);

    }



    /**
     * 多条件查询消息
     *
     * @param sysMsg the sys msg
     * @param start  the start
     * @param end    the end
     * @return the model and view
     */
    @RequestMapping("/seclectbyterms")
    @ResponseBody
    public String selectBy(SysMsg sysMsg,Date start,Date end){
        Map map=new HashMap();
        map.put("start",start);
        map.put("end",end);
        map.put("sysMsg",sysMsg);
        List<SysMsg> listMsg=isms.selectBy(map);
        /*Map<String, Object> ssmMap = new HashMap<>();*/

       String jsonString = JSON.toJSONString(listMsg);
        /*ssmMap.put("ssm", jsonString);*/
      /*  logger.info(jsonString+"============");*/
       logger.info(jsonString+"======");
        return jsonString;
    }

    /**
     * 发布消息
     *
     * @param sysMsg the sys msg
     * @return the int
     */
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(SysMsg sysMsg) {
        Map<String, Object> map = new HashMap<String, Object>();
        sysMsg.setPublishTime(new Date());
        int flag=isms.insert(sysMsg);
        String msg=null;
        if (flag==1){
            msg="SUCCESSFULLY";
        }else {
            msg="FAILURE";
        }
            return msg;

    }
    /**
     * 发布消息页面
     *
     * @param
     * @return the int
     */
    @RequestMapping("/insertMsgPage")
    public ModelAndView insertPro() {


        return new ModelAndView("msgInsertPage");
    }

    /**
     * 删除消息
     *
     * @param sysMsg the sys msg
     * @return the int
     */
    @RequestMapping("/delById")
    @ResponseBody
    public String del(SysMsg sysMsg) {

        logger.info(sysMsg.getId()+"======");
        int flag=isms.deleteByPrimaryKey(sysMsg.getId());
        String msg=null;
        logger.info(flag+"======");
        if (flag==1){
            msg="SUCC";
        }else {
            msg="";
        }
        return msg;

    }


}
